package com.yyds.service.impl;

import com.github.pagehelper.PageHelper;
import com.yyds.common.MyJSON;
import com.yyds.common.R;
import com.yyds.mapper.PreventRegionMapper;
import com.yyds.pojo.PreventRegion;
import com.yyds.service.PreventRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("PreventRegionService")
public class PreventRegionServiceImpl implements PreventRegionService {
    @Autowired
    private PreventRegionMapper preventRegionMapper;

    public void setPreventRegionMapper(PreventRegionMapper preventRegionMapper) {
        this.preventRegionMapper = preventRegionMapper;
    }

    @Override
    public MyJSON<PreventRegion> selectAllPreventRegionService(PreventRegion preventRegion, int page, int limit) {
        System.out.println("通过分页查询防控的全部信息 : page= "+page+" , limit= "+limit);
        //总数据条数
        int count = preventRegionMapper.findPreventRegionMapper(preventRegion).size();
        PageHelper.startPage(page,limit);
        //分页后的数据
        List<PreventRegion> list = preventRegionMapper.findPreventRegionMapper(preventRegion);
        MyJSON<PreventRegion> newsMyJSON = new MyJSON<>();
        newsMyJSON.setCode(0);
        newsMyJSON.setMsg("success");
        newsMyJSON.setCount((long)count);
        newsMyJSON.setData(list);
        return newsMyJSON;
    }
    //添加
    @Override
    public R insertPreventRegionService(PreventRegion preventRegion) {
        int i = preventRegionMapper.insertPreventRegionMapper(preventRegion);
        if (i > 0 ) {
            return R.ok().message("添加成功");
        }else{
            return R.error().message("添加失败");
        }
    }
    //删除
    @Override
    public int deletePreventRegionServiceById(Integer region_id) {

        return preventRegionMapper.deletePreventRegionServiceByIdMapper(region_id);
    }
    //修改
    @Override
    public int updatePreventRegionServiceById(PreventRegion preventRegion) {

        return preventRegionMapper.updatePreventRegionServiceByIdMapper(preventRegion);
    }
    //删除部分
    @Override
    public Integer deleteAllPreventRegion(String str) {
        String[] strs = str.split("-");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < strs.length; i++) {
            list.add(Integer.parseInt(strs[i]));
        }
        return preventRegionMapper.deletesPreventRegion(list);
    }

}
